package org.jeecg.modules.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class CommonServiceUtil {

    public static JSONObject cityLookup(String location, String key) {
        String url = "https://geoapi.qweather.com/v2/city/lookup?location=" + location + "&key=" + key;
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                HttpClientBuilder.create().build());
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        ResponseEntity<Object> forEntity = restTemplate.getForEntity(url, Object.class);
        return new JSONObject((Map<String, Object>) forEntity.getBody());
    }

    public static JSONObject weatherNow(String location, String key) {
        return weatherNow(location, key, true);
    }

    public static JSONObject weatherNow(String location, String key, boolean idDev) {
        JSONObject jsonObject = cityLookup(location, key);
        String locationString = jsonObject.getJSONArray("location").getJSONObject(0).getString("id");
        String dev = "https://devapi.qweather.com/v7/weather/now?location=";
        String com = "https://devapi.qweather.com/v7/weather/now?location=";
        String url = (idDev ? dev : com) + locationString + "&key=" + key;
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                HttpClientBuilder.create().build());
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        ResponseEntity<Object> forEntity = restTemplate.getForEntity(url, Object.class);
        return new JSONObject((Map<String, Object>) forEntity.getBody());
    }

    public static JSONObject weatherNowById(String id, String key) {
        String url = "https://devapi.qweather.com/v7/weather/now?location=" + id + "&key=" + key;
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                HttpClientBuilder.create().build());
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        ResponseEntity<Object> forEntity = restTemplate.getForEntity(url, Object.class);
        return new JSONObject((Map<String, Object>) forEntity.getBody());
    }

    public static JSONObject weatherNowByName(String location, String key) {
        JSONObject jsonObject = cityLookup(location, key);
        String locationString = jsonObject.getJSONArray("location").getJSONObject(0).getString("id");
        return weatherNowById(locationString, key);
    }

    public static JSONObject weatherForecast7ById(String id, String key) {
        String dev = "https://devapi.qweather.com/v7/weather/7d?location=";
        String url = dev + id + "&key=" + key;
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                HttpClientBuilder.create().build());
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        ResponseEntity<Object> forEntity = restTemplate.getForEntity(url, Object.class);
        return new JSONObject((Map<String, Object>) forEntity.getBody());
    }

    public static JSONObject weatherForecast7ByName(String location, String key) {
        JSONObject jsonObject = cityLookup(location, key);
        String id = jsonObject.getJSONArray("location").getJSONObject(0).getString("id");
        return weatherForecast7ById(id, key);
    }

    public static void main(String[] args) {
//        JSONObject jsonObject = weatherForecast7ById("101120512", "aaab13d3350944ad8e1ebf0a6714c37a");
//        JSONObject jsonObject = weatherForecast7ById("101120512", "9aa160077aee46288937cf5fae726af8");
//        JSONObject jsonObject = weatherNowById("101120512", "9aa160077aee46288937cf5fae726af8");
        JSONObject jsonObject = cityLookup("烟台", "9aa160077aee46288937cf5fae726af8");
        System.out.println(jsonObject);
    }

}
